// pages/warnTime/warnTime.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 0, // 1 时间点 2 时间段
    timeShow: false,
    timeFrameShow:false, // 时间段
    defultTime: "12:00",
    timeList: [],
    timeFrameStart: "",
    timeFrameEnd: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        type: options.type
      })
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let warnTime = wx.getStorageSync('warnTime')
    let timeList = []
    if(warnTime){
      timeList = warnTime.split(",")
    }
    
    this.setData({
      timeList
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.shareTitle,
      path: '/pages/index/index'
    }
  },
  openTimeShow(){
    if(this.data.type == 1){ // 1 时间点 2时间段
      this.setData({
        timeShow: true
      })
    }else{
      this.setData({
        timeFrameShow: true
      })
    }

  },
  onClose(){
    this.setData({
      timeShow: false,
      timeFrameShow: false
    })
  },
  timeConfirm(e){
    let time = e.detail
    let timeList = this.data.timeList
    timeList.push(time)
    this.setData({
      timeList,
      timeShow: false
    }) 
  },
  sub(e){
    let idx = e.currentTarget.dataset.idx
    let timeList = this.data.timeList
    timeList.splice(idx,1)
    this.setData({
      timeList
    })
  },
  timeStartChange(e){
    this.setData({
      timeFrameStart: e.detail
    })
  },
  timeEndChange(e) {
    this.setData({
      timeFrameEnd: e.detail
    })
  },
  timeFrameConfirm(){
    let timeList = this.data.timeList
    timeList.push(this.data.timeFrameStart + "-" + this.data.timeFrameEnd)
    this.setData({
      timeList,
      timeFrameShow: false
    }) 
  },
  submit(){
    app.globalData.warnTime = this.data.timeList.join(",")
    wx.navigateBack()
  }
})